﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrNhapChiTiet : DevComponents.DotNetBar.Office2007Form
    {
        public FrNhapChiTiet()
        {
            InitializeComponent();
        }
        private float ChieuDai1, ChieuRong1, ChieuDay1, SoLuong1;
        private double SoKhoi1,TongKhoi1;
        private string NguonCungCap1,DVT1,Serial1,ChungTu1,GhiChu1,child=" - ";
        public void Display_XK()
        {
            dataGridView1.Columns["SoLuong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["SoKhoi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;//Hien thi text ben lề phải của gridview
            //dataGridView1.Columns["ThanhTien"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;//Hien thi text ben lề phải của gridview 
            dataGridView1.Columns["NguonCungCap"].Visible = false;
            dataGridView1.Columns["SoKhoi"].Width = 180;
            dataGridView1.Columns["GhiChu"].Width = 195;
            cb_NguonCungCap.Enabled = false;
        }
        public void Display_LS()
        {
            dataGridView1.Columns["SoLuong"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["SoKhoi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            
        }
        public void Get_Serial(string s)
        {
            txt_Serial.Text = s;
        }

        public void User(string User)
        {
            lb_User.Text = User;
        }
        private void combo_load()
        {
            FormProcess Frp = new FormProcess();
            string sql_NCC = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_NguonCungCap, sql_NCC, 0, 1);
        }
        public void ChungTu(string CT)
        {
            txt_ChungTu.Text = CT;
        }

        private void txt_W_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_D);
        }

        private void txt_L_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_R);
        }

        private void txt_H_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_L);
        }

        private void txt_SoLuong_TextChanged(object sender, EventArgs e)
        {
            FormProcess Frp = new FormProcess();
            Frp.checktext_number(txt_SoLuong);
        }
        private void insert_gridview()
        {
            if (txt_D.Text == "" || txt_R.Text == "" || txt_L.Text == "" || txt_SoLuong.Text == "")
                MessageBox.Show("Nhập thiếu thông tin. Vui lòng kiểm tra lại");
            else
            {
                Process prs = new Process();
                FormProcess Frp = new FormProcess();
                try
                {
                    if (cb_NguonCungCap.Text != "")
                        NguonCungCap1 = prs.substring_right(cb_NguonCungCap.Text, child);
                    else
                        NguonCungCap1 = cb_NguonCungCap.Text;
                    GhiChu1 = txt_GhiChu.Text;
                    if (Frp.isfloat(txt_D.Text))
                        ChieuDay1 = float.Parse(txt_D.Text);
                    else
                        ChieuDay1 = 0;
                    if (Frp.isfloat(txt_R.Text))
                        ChieuRong1 = float.Parse(txt_R.Text);
                    else
                        ChieuRong1 = 0;
                    if (Frp.isfloat(txt_L.Text))
                        ChieuDai1 = float.Parse(txt_L.Text);
                    else
                        ChieuDai1 = 0;
                    if (Frp.isfloat(txt_SoLuong.Text))
                        SoLuong1 = float.Parse(txt_SoLuong.Text);
                    else
                        SoLuong1 = 0;
                    SoKhoi1 = (ChieuRong1 * ChieuDai1 * ChieuDay1 * SoLuong1) / 1000000000;
                    dataGridView1.Rows.Add(NguonCungCap1, ChieuDay1, ChieuRong1, ChieuDai1, "mm", SoLuong1, SoKhoi1, GhiChu1);
                    TongKhoi1 = double.Parse(lb_TongKhoi.Text);
                    TongKhoi1 += SoKhoi1;
                    lb_TongKhoi.Text = TongKhoi1.ToString();
                    txt_SoLuong.Text = "";
                    txt_L.Text = "";
                    txt_R.Text = "";
                    txt_D.Text = "";
                    txt_GhiChu.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }
        private void bt_insert_Click(object sender, EventArgs e)
        {
            try
            {
                insert_gridview();
                bt_save.Enabled = true;
                int j = dataGridView1.Rows.Count - 1;
                dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                dataGridView1.FirstDisplayedCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                txt_D.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void FrNhapChiTiet_Load(object sender, EventArgs e)
        {
            combo_load();
            //Display();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 1)
            {
                try
                {
                    int r = dataGridView1.CurrentCell.RowIndex;
                    TongKhoi1 = double.Parse(lb_TongKhoi.Text) - double.Parse(dataGridView1.Rows[r].Cells["SoKhoi"].Value.ToString());
                    lb_TongKhoi.Text = string.Format("{0:###,###.#########}", TongKhoi1);
                    dataGridView1.Rows.RemoveAt(r);
                    int j = dataGridView1.Rows.Count - 1;
                    dataGridView1.CurrentCell = dataGridView1.Rows[j].Cells["ChieuRong"];
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (dataGridView1.Rows.Count == 1)
                lb_TongKhoi.Text = "0";
        }

        private void bt_delete_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    dataGridView1.Rows.RemoveAt(i);
                }
                lb_TongKhoi.Text = "0";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }     
        }
        private void Save()
        {
            int i;
            float SoKhoi2;
            Data dta = new Data();
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                {
                    ChieuRong1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuRong"].Value.ToString());
                    ChieuDai1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDai"].Value.ToString());
                    ChieuDay1 = float.Parse(dataGridView1.Rows[i].Cells["ChieuDay"].Value.ToString());
                    SoLuong1 = float.Parse(dataGridView1.Rows[i].Cells["SoLuong"].Value.ToString());
                    SoKhoi2 = float.Parse(dataGridView1.Rows[i].Cells["SoKhoi"].Value.ToString());
                    DVT1 = dataGridView1.Rows[i].Cells["DVT"].Value.ToString();
                    NguonCungCap1 = dataGridView1.Rows[i].Cells["NguonCungCap"].Value.ToString();
                    GhiChu1 = dataGridView1.Rows[i].Cells["GhiChu"].Value.ToString();
                    Serial1 = txt_Serial.Text;
                    ChungTu1 = txt_ChungTu.Text;
                    string insert_NK = @"insert into dbo.NhapKhoGo(Serial_NK,ChungTu,NguonCungCap,ChieuDay,ChieuRong,ChieuDai,SoKhoi,SoLuong,DVT,GhiChu) values(";
                    insert_NK += Serial1 + ",N'" + ChungTu1 + "',N'" + NguonCungCap1 + "'," + ChieuDay1 + "," + ChieuRong1 + "," + ChieuDai1 + "," + SoKhoi2 + "," + SoLuong1 + ",'" + DVT1 + "',N'" + GhiChu1 + "')";
                    //MessageBox.Show(insert_NK);
                    cmd.CommandText = insert_NK;
                    cmd.ExecuteNonQuery();
                    dataGridView1.Rows.RemoveAt(i);
                }
                MessageBox.Show("Dữ liệu được lưu thành công");
                this.Close();
            }
            catch (Exception ex)
            {
                string delete_Serial = @"delete from dbo.Serial_NKGo where Serial_NK=" + Serial1;
                cmd.CommandText = delete_Serial;
                cmd.ExecuteNonQuery();
                MessageBox.Show("Chứng từ này sẽ không insert vào DataBase do lỗi nhập liệu " + ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                this.Close();
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void FrNhapChiTiet_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F1)
                bt_insert_Click(null, null);
            if (ex.KeyCode == Keys.F11)
                Save();
        }
    }
}
